package controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.UserDAO;
import model.User;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		UserDAO dao = new UserDAO();
		User user = dao.getUser(username, password);
		if(user == null){//登录失败
			System.out.println("Wrong");
		}else{
			if(user.getUserLimit().equals("staff")){//制单员
				System.out.println("staff");
				request.getSession().setAttribute("user", user);
				response.sendRedirect("./view/jinhuo.jsp");
			}else{
				if(user.getUserLimit().equals("manager")){//管理员
					System.out.println("manager");
					request.getSession().setAttribute("user", user);
					response.sendRedirect("./JinhuoshenheController");
				}else{//登录失败
					System.out.println("Wrong");
				}
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
